package com.gdzl_hadoop.common;

import com.google.common.cache.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * Created by 1032019725 on 2017/10/17.
 */
public class TokenCache {

    private static Logger logger = LoggerFactory.getLogger(TokenCache.class);
    //token前缀名
    public static final String TOKEN_PREFIX = "token_";
    //LRU算法,当容量超过1000时会使用LRU置换算法
    private static LoadingCache<String, String> localCache = CacheBuilder.newBuilder().initialCapacity(1000).maximumSize(10000).expireAfterAccess(12, TimeUnit.HOURS)
            .build(new CacheLoader<String, String>() {
                //默认的数据加载实现，当调用get取值的时候，如果key没有对应的值，就调用这个方法进行加载
                @Override
                public String load(String s) throws Exception {
                    return null;
                }
            });

    public static void setKey(String key,String value){
        localCache.put(key,value);
    }

    public static String getKey(String key){
        String value = null;
        try{
            value = localCache.get(key);
            if("null".equals(value)){
                return  null;
            }
            return value;
        }catch (Exception ex){
            logger.error("localCache get error",ex);
        }
        return  null;
    }

}

/*
public class CacheMap {
    private static final Logger log = LoggerFactory.getLogger(CacheMap.class);

    */
/**
 * @desction: 使用google guava缓存处理
 * @author: wangji
 * @date: 2017/11/22 9:59
 * @desction: 获取缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 放入缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 移除缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 批量删除缓存
 * @author: wangji
 * @date: 2017/11/22 9:49
 * @desction: 获取缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 放入缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 移除缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 批量删除缓存
 * @author: wangji
 * @date: 2017/11/22 9:49
 * @desction: 获取缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 放入缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 移除缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 * @desction: 批量删除缓存
 * @author: wangji
 * @date: 2017/11/22 9:49
 *//*

private static Cache<String,Object> cache;
static {
        cache = CacheBuilder.newBuilder().maximumSize(10000)
        .expireAfterWrite(24, TimeUnit.HOURS)
        .initialCapacity(10)
        .removalListener(new RemovalListener<String, Object>() {
@Override
public void onRemoval(RemovalNotification<String, Object> rn) {
        if(log.isInfoEnabled()){
        log.info("被移除缓存{}:{}",rn.getKey(),rn.getValue());
        }
        }
        }).build();
        }

*/
/**
 * @desction: 获取缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 *//*

public  static Object get(String key){
        return StringUtils.isNotEmpty(key)?cache.getIfPresent(key):null;//使用getIfPresent就不需要手动CacheLoader  refreshAfterWrite(2, TimeUnit.SECONDS) // 2s后重新刷新
        }
*/
/**
 * @desction: 放入缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 *//*

public static void put(String key,Object value){
        if(StringUtils.isNotEmpty(key) && value !=null){
        cache.put(key,value);
        }
        }
*/
/**
 * @desction: 移除缓存
 * @author: wangji
 * @date: 2017/11/22 9:50
 *//*

public static void remove(String key){
        if(StringUtils.isNotEmpty(key)){
        cache.invalidate(key);
        }
        }
*/
/**
 * @desction: 批量删除缓存
 * @author: wangji
 * @date: 2017/11/22 9:49
 *//*

public static void remove(List<String> keys){
        if(keys !=null && keys.size() >0){
        cache.invalidateAll(keys);
        }
        }
        }
*/

